package nl.justobjects.pushlet.wrapper.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.pushmanager.PushManager.PushManageCommand;
import nl.justobjects.pushlet.wrapper.JMSService;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class JMSServiceImpl implements JMSService{
	JmsTemplate jmsTemplate;

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
	
	/**
	 * 广播推送
	 * @param subject
	 * @param label
	 * @param data
	 */
	@Override
	public void broadcastEvent(final String subject, final String label, final Map<String, Serializable> data) {
		MessageCreator messageCreator = new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				Message msg = session.createMapMessage();
				msg.setObjectProperty("data", data);
				msg.setStringProperty("subject", subject);
				if (label != null) {
					msg.setStringProperty("label", label);
				}
				return msg;
			}
		};
		this.jmsTemplate.send(messageCreator);
	}
	
	/**
	 * 多播推送
	 * @param pids
	 * @param subject
	 * @param label
	 * @param data
	 */
	@Override
	public void multicastEvent(final List<String> pids, final String subject, 
			final String label, final Map<String, Serializable> data) {
		MessageCreator messageCreator = new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				Message msg = session.createMapMessage();
				msg.setObjectProperty("data", data);
				msg.setStringProperty("subject", subject);
				if (label != null) {
					msg.setStringProperty("label", label);
				}
				msg.setObjectProperty("pids", pids);
				return msg;
			}
		};
		this.jmsTemplate.send(messageCreator);
	}
	
	/**
	 * 单播推送
	 * @param pid
	 * @param subject
	 * @param label
	 * @param data
	 */
	@Override
	public void unicastEvent(final String pid, final String subject, final String label,
			final Map<String, Serializable> data) {
		MessageCreator messageCreator = new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				Message msg = session.createMapMessage();
				msg.setObjectProperty("data", data);
				msg.setStringProperty("subject", subject);
				if (label != null) {
					msg.setStringProperty("label", label);
				}
				List<String> pids = new ArrayList<String>();
				pids.add(pid);
				msg.setObjectProperty("pids", pids);
				return msg;
			}
		};
		this.jmsTemplate.send(messageCreator);
	}

	@Override
	public void broadcastEvent(final Event event) {
		this.jmsTemplate.convertAndSend(event);
	}
	
	@Override
	public void broadcastPushManageCommand(final PushManageCommand command) {
		this.jmsTemplate.convertAndSend(command);
	}

	@Override
	public void multicastEvent(final List<String> pids, final Event event) {
		MessageCreator messageCreator = new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				Message msg = session.createMapMessage();
				msg.setObjectProperty("event", event.getAttributes());
				msg.setObjectProperty("pids", pids);
				return msg;
			}
		};
		this.jmsTemplate.send(messageCreator);
	}
}
